//
//  HYTypeCell.m
//  HaiYue
//
//  Created by skyline on 2018/5/8.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYTypeCell.h"
#import "HYGameTypeModel.h"
NSString *const HYTypeCellItemClick = @"HYTypeCellItemClick";
@implementation HYTypeCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void)setModel:(HYGameTypeModel *)model
{
    if (_model != model)
    {
        _model = model;
        [self setupItem];
    }
}

- (void)setGrids:(NSMutableArray *)grids
{
    if (_grids != grids)
    {
        _grids = grids;
        [self setupItem];
    }
}

- (void)setupItem
{
    NSMutableArray *arr = (self.model)?self.model.grids:self.grids;
    
    CGFloat width = (SCREEN_MIN_LENGTH - GAP * 2 - HYTypeCellItemGap * 3) / 4.0;
    for (int i = 0; i < arr.count; i++)
    {
        NSString *subItem = arr[i];
        NSInteger row = i % 4;
        NSInteger column = i / 4;
        HYButton *button = [[HYButton alloc] initWithTitle:subItem backColor:LINE_COLOR];
        button.tag = 200 + i;
        button.layer.borderWidth = 0.5;
        button.layer.borderColor = CELL_LINE_COLOR.CGColor;
        button.touchColor = THEME_COLOR;
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        [button setTitleColor:TEXT_GRAY_COLOR forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [button addTarget:self action:@selector(actionForSearchItem:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(GAP + column * (HYTypeCellItemHeight + GAP));
            make.left.equalTo(self).with.offset(GAP + row * (width + HYTypeCellItemGap));
            make.size.mas_offset(CGSizeMake(width, HYTypeCellItemHeight));
        }];
    }
    self.bounds = CGRectMake(0, 0, SCREEN_MIN_LENGTH, (ceil(arr.count / 4.0)) * (HYTypeCellItemHeight + GAP) + GAP);
}

- (void)actionForSearchItem:(HYButton *)sender
{
    if (self.model)
        [CCNotice post:HYTypeCellItemClick obj:nil info:@{@"property":self.model.prop,@"name":self.model.grids[sender.tag - 200]}];
    else
        [CCNotice post:HYTypeCellItemClick obj:nil info:@{@"name":self.grids[sender.tag - 200]}];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
